package com.shensi.adminCollect.controller.station;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.domain.station.Grade;
import com.shensi.adminCollect.domain.station.Station;
import com.shensi.adminCollect.service.station.IGradeService;
import com.shensi.adminCollect.service.station.IOilService;
import com.shensi.adminCollect.service.station.IStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

/**
 * 油站信息Controller
 *
 * @author qujia
 * @date 2020-12-08
 */
@RestController
@RequestMapping("/grade/info")
public class GradeController extends BaseController
{
    @Autowired
    private IGradeService gradeService;
    @Autowired
    private IStationService stationService;
    @Autowired
    private IOilService oilService;
    /**
     * 查询等级信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Grade grade)
    {
        grade.setDeleteFlag("0");
        LoginUser current=SecurityUtils.getLoginUser();
        if("1".equals(current.getUser().getUserLevel())){
            grade.setEnterpriseId(current.getUser().getEnterpriseId());
        }else if("2".equals(current.getUser().getUserLevel())) {
            if(current.getUser().getStationId()!=null){
                Station station=stationService.selectStationById(current.getUser().getStationId());
                grade.setEnterpriseId(station.getEnterpriseId());
            }
        }else {
            return getDataTable(Collections.emptyList());
        }
        startPage();
        List<Grade> gradeList = gradeService.selectGradeList(grade);
        return getDataTable(gradeList);
    }

    /**
     * 新增油品
     */
    @PostMapping
    public AjaxResult add(@RequestBody Grade grade)
    {
        LoginUser current=SecurityUtils.getLoginUser();
        if(!"1".equals(current.getUser().getUserLevel())){
            return  AjaxResult.error("油企才能配置等级信息");
        }
        Grade grade1=new Grade();
        grade1.setDeleteFlag("0");
        grade1.setEnterpriseId(current.getUser().getEnterpriseId());
        List<Grade> gradeList=gradeService.selectGradeList(grade1);
        if(gradeList!=null){
            if(gradeList.size()>5){
                return  AjaxResult.error("最多允许五个等级");
            }
        }
        grade.setEnterpriseId(current.getUser().getEnterpriseId());
        grade.setCreateDate(new Date());
        grade.setCreateUser(current.getUser().getUserId());
        return toAjax(gradeService.insertGrade(grade));
    }
    /**
     * 删除油品
     */
    @PostMapping(value = "/deleteGrade")
    public AjaxResult deleteGrade(@RequestBody Grade grade)
    {
        LoginUser current=SecurityUtils.getLoginUser();
        if(!"1".equals(current.getUser().getUserLevel())){
            return  AjaxResult.error("油企才能配置等级信息");
        }
        grade.setEnterpriseId(current.getUser().getEnterpriseId());
        grade.setDeleteFlag("0");
        grade.setDeleteDate(new Date());
        grade.setDeleteUser(current.getUser().getUserId());
        return toAjax(gradeService.updateGrade(grade));
    }



    /**
     * 获取等级信息详细信息
     */
    @GetMapping(value = "/{gradeId}")
    public AjaxResult getInfo(@PathVariable("gradeId") Long gradeId)
    {
        Grade grade=gradeService.selectGradeById(gradeId);

        return AjaxResult.success(grade);
    }

    /**
     * 修改油站信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Grade grade)
    {
        LoginUser current=SecurityUtils.getLoginUser();
        if(!"1".equals(current.getUser().getUserLevel())){
            return  AjaxResult.error("油企才能配置等级信息");
        }
        grade.setEnterpriseId(current.getUser().getEnterpriseId());
        return toAjax(gradeService.updateGrade(grade));
    }



}
